﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SRM.DataObjects;
using SRM;

public partial class UserControls_PersonalParticularsUserControl : System.Web.UI.UserControl, SRM.IUserInput
{
    public DataTable PersonalParticularsDataTable { get; set; }

    public DataRow row { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        int parsedInt = 0;

        if (!Page.IsPostBack)
        {
            this.row = null;
            //Page.DataBind();

            //this.ApplicantKey = Session["ApplicantKey"].ToString();
            //this.hdn_applicantKey.Value = Session["ApplicantKey"].ToString();
            int applicantKey = int.Parse(Session["ApplicantKey"].ToString());
            this.PersonalParticularsDataTable = SRM_Applicant.GetPersonalParticulars(applicantKey.ToString());
            if ((this.PersonalParticularsDataTable != null) && (this.PersonalParticularsDataTable.Rows.Count > 0))
                this.row = this.PersonalParticularsDataTable.Rows[0];

            if (this.row == null)
                return;

            // Start filling in details on the form
            // VacancyMedium, AgencyName
            this.JobReferrerSource_RadioButtonList.SelectedValue = this.row.Field<string>("VacancyMedium");
            this.AgencyName_RadTextBox.Text = this.row.Field<string>("AgencyName");
            if ((this.JobReferrerSource_RadioButtonList != null) &&
                (this.JobReferrerSource_RadioButtonList.SelectedValue == "Agency"))
            {
                this.AgencyName_Panel.Visible = true;
            }
            else
            {
                this.AgencyName_RadTextBox.Text = string.Empty;
                this.AgencyName_Panel.Visible = false;
            }

            // Name
            this.Salutation_RadioButtonList.SelectedValue = this.row.Field<string>("Salutation");
            this.GivenName_TextBox.Text = this.row.Field<string>("GivenName");
            this.Surname_TextBox.Text = this.row.Field<string>("LastName");
            this.NameFormat_DropDownList.SelectedValue = this.row.Field<string>("NameFormat");
            this.ChineseCharacter_TextBox.Text = this.row.Field<string>("ChineseName");
            UpdateDisplayName();

            // Identity
            this.IdentityNo_TextBox.Text = this.row.Field<string>("NRICNo");
            //this.IdentityType_DropDownList.SelectedValue = Help.GetListItemValue(this.IdentityType_DropDownList.Items, this.row.Field<string>("NRICType"));
            this.IdentityType_DropDownList.SelectedValue = this.row.Field<string>("NRICType");
            this.IssuingDate_RadDatePicker.SelectedDate = this.row.Field<DateTime?>("NRICIssuingDate");
            

            this.Citizenship_DropDownList.SelectedValue = this.row.Field<string>("Citizenship");
            this.PlaceOfBirth_DropDownList.SelectedValue = this.row.Field<string>("PlaceOfBirth");
            //this.CountryOfBirth_DropDownList.SelectedValue = this.row.Field<string>("CountryCode");
            this.DrivingLicenseClass_DropDownList.SelectedValue = this.row.Field<string>("DrivingClass");

            // Address
            this.BlockOrHouseNo_TextBox.Text = this.row.Field<string>("BlockNo");
            this.Floor_TextBox.Text = this.row.Field<string>("LevelNo");
            this.UnitNo_TextBox.Text = this.row.Field<string>("UnitNo");
            this.StreetName_TextBox.Text = this.row.Field<string>("StreetName");
            this.BuildingName_TextBox.Text = this.row.Field<string>("BuildingName");

            if (int.TryParse(this.row.Field<string>("PostalCode"), out parsedInt))
                this.PostalCode_TextBox.Text = parsedInt.ToString();
            else
                this.PostalCode_TextBox.Text = string.Empty;

            this.Country_DropDownList.SelectedValue = this.row.Field<string>("CountryCode");
            this.City_TextBox.Text = this.row.Field<string>("City");
            this.State_TextBox.Text = this.row.Field<string>("State");
            this.ContactNoM_TextBox.Text = this.row.Field<string>("MobileNo");
            this.ContactNoH_TextBox.Text = this.row.Field<string>("HomeNo");

            
            // Working Experience
            this.WorkingExperience_TextBox.Text = this.row.Field<int?>("YrsExperience").ToString();
            this.PeriodOfNotice_TextBox.Text = this.row.Field<decimal?>("PeriodNotice").ToString();
            this.ApproxAvailabilityDate_RadDatePicker.SelectedDate = this.row.Field<DateTime?>("Availability");
            this.ExpectedMonthlyBasicSalary_TextBox.Text = this.row.Field<decimal?>("AskingSalary").ToString();

            //this.Pdpa_CheckBox.Checked = this.row.Field<DateTime?>("PDPAConsent").HasValue;

        }
    }

    protected void JobReferrerSource_RadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadioButtonList JobReferrerSource_RadioButtonList = sender as RadioButtonList;

        if ((JobReferrerSource_RadioButtonList != null) &&
            (JobReferrerSource_RadioButtonList.SelectedValue == "Agency"))
        {
            this.AgencyName_Panel.Visible = true;
        }
        else
        {
            this.AgencyName_Panel.Visible = false;
        }
    }

    protected void NameFormat_DropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        UpdateDisplayName();
    }
    
    protected void GivenName_TextBox_TextChanged(object sender, EventArgs e)
    {
        UpdateDisplayName();
    }
    
    protected void Surname_TextBox_TextChanged(object sender, EventArgs e)
    {
        UpdateDisplayName();
    }

    private void UpdateDisplayName()
    {
        if (!string.IsNullOrEmpty(this.NameFormat_DropDownList.SelectedValue))
        {
            string surname = this.Surname_TextBox.Text.Trim();
            string givenName = this.GivenName_TextBox.Text.Trim();
            string comma = string.Empty;

            if (this.NameFormat_DropDownList.SelectedValue == "00")
            {
                if (!string.IsNullOrEmpty(givenName))
                    comma = ", ";
                this.DisplayNameResult_Literal.Text = string.Format("{0}{1}{2}",
                    this.Surname_TextBox.Text.Trim(), comma, this.GivenName_TextBox.Text.Trim());
            }
            if (this.NameFormat_DropDownList.SelectedValue == "01")
            {
                if (!string.IsNullOrEmpty(surname))
                    comma = ", ";
                this.DisplayNameResult_Literal.Text = string.Format("{0}{1}{2}",
                    this.GivenName_TextBox.Text.Trim(), comma, this.Surname_TextBox.Text.Trim());
            }
        }
    }

    protected void JobReferrerSource_RadComboBox_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
        if (e.Value == "2")
        {
            this.AgencyName_Panel.Visible = true;
        }
        else
        {
            this.AgencyName_Panel.Visible = false;
        }
    }

    //protected void JobReferrerSource_DropDownList_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    if (JobReferrerSource_DropDownList.SelectedValue == "2")
    //    {
    //        this.AgencyName_Panel.Visible = true;
    //    }
    //    else
    //    {
    //        this.AgencyName_Panel.Visible = false;
    //    }
    //}
    //protected void JobReferrerSource_RadComboBox_TextChanged(object sender, EventArgs e)
    //{
    //    if (JobReferrerSource_DropDownList.SelectedValue == "2")
    //    {
    //        this.AgencyName_Panel.Visible = true;
    //    }
    //    else
    //    {
    //        this.AgencyName_Panel.Visible = false;
    //    }
    //}

    protected void JobReferrerSource_RadioButtonList_TextChanged(object sender, EventArgs e)
    {
        if ((JobReferrerSource_RadioButtonList != null) &&
            (JobReferrerSource_RadioButtonList.SelectedValue == "Agency"))
        {
            this.AgencyName_Panel.Visible = true;
        }
        else
        {
            this.AgencyName_Panel.Visible = false;
        }
    }

    public void SaveUserInput()
    {
        string applicantKey = string.Empty;

        if (Page is IApplicantForm)
        {
            applicantKey = ((IApplicantForm)Page).ApplicantKey;

            SRM_Applicant.UpdatePersonalParticulars(applicantKey,
                this.JobReferrerSource_RadioButtonList.SelectedValue,
                this.AgencyName_RadTextBox.Text,
                this.Salutation_RadioButtonList.SelectedValue,
                this.GivenName_TextBox.Text,
                this.Surname_TextBox.Text,
                this.NameFormat_DropDownList.SelectedValue,
                this.ChineseCharacter_TextBox.Text,
                this.IdentityNo_TextBox.Text,
                this.IdentityType_DropDownList.SelectedValue,
                this.IssuingDate_RadDatePicker.SelectedDate,
                string.Empty,
                this.Citizenship_DropDownList.SelectedValue,
                this.PlaceOfBirth_DropDownList.SelectedValue,
                this.DrivingLicenseClass_DropDownList.SelectedValue,
                this.BlockOrHouseNo_TextBox.Text,
                this.Floor_TextBox.Text,
                this.UnitNo_TextBox.Text,
                this.StreetName_TextBox.Text,
                this.BuildingName_TextBox.Text,
                this.PostalCode_TextBox.Text,
                this.Country_DropDownList.SelectedValue,
                this.City_TextBox.Text,
                this.State_TextBox.Text,
                this.ContactNoM_TextBox.Text,
                this.ContactNoH_TextBox.Text,
                this.WorkingExperience_TextBox.Text,
                this.PeriodOfNotice_TextBox.Text,
                this.ApproxAvailabilityDate_RadDatePicker.SelectedDate,
                this.ExpectedMonthlyBasicSalary_TextBox.Text
                );
        }
    }
}